package com.ruoyi.web.controller.category;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.category.domain.SysPropTerrain;
import com.ruoyi.category.mapper.SysPropTerrainMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.category.domain.SysPorp;
import com.ruoyi.category.service.ISysPorpService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物品信息Controller
 * 
 * @author yjf
 * @date 2022-07-07
 */
@RestController
@RequestMapping("/category/porp")
public class SysPorpController extends BaseController
{
    @Autowired
    private ISysPorpService sysPorpService;

    @Autowired
    private SysPropTerrainMapper propTerrainMapper;

    /**
     * 查询物品信息列表
     */
    @PreAuthorize("@ss.hasPermi('category:porp:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPorp sysPorp)
    {
        //查询物品-地图关系列表
        SysPropTerrain sysPropTerrain = new SysPropTerrain();
        List<SysPropTerrain> propTerrainList = propTerrainMapper.selectPropTerrain(sysPropTerrain);
        startPage();
        //物品列表
        List<SysPorp> porpList = sysPorpService.selectSysPorpList(sysPorp);
        //构建地图名称
        for(SysPorp prop : porpList){
            StringBuffer terrain = new StringBuffer();
            String terrainName ="";
            for(SysPropTerrain propTerrain : propTerrainList){
                if(prop.getPropId().equals(propTerrain.getPropId())){
                    terrain.append(",");
                    terrain.append(propTerrain.getTerrainName());
                }
            }
            if(StringUtils.isNotBlank(terrain.toString())){
             terrainName = terrain.toString().substring(1);
            }
            prop.setTerrainNameStr(terrainName);
        }
        return getDataTable(porpList);
    }

    /**
     * 获取物品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('category:porp:query')")
    @GetMapping(value = "/{propId}")
    public AjaxResult getInfo(@PathVariable("propId") Long propId)
    {
        return AjaxResult.success(sysPorpService.selectSysPorpByPropId(propId));
    }

    /**
     * 新增物品信息
     */
    @PreAuthorize("@ss.hasPermi('category:porp:add')")
    @Log(title = "物品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPorp sysPorp)
    {
        return toAjax(sysPorpService.insertSysPorp(sysPorp));
    }

    /**
     * 修改物品信息
     */
    @PreAuthorize("@ss.hasPermi('category:porp:edit')")
    @Log(title = "物品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPorp sysPorp)
    {
        return toAjax(sysPorpService.updateSysPorp(sysPorp));
    }

    /**
     * 删除物品信息
     */
    @PreAuthorize("@ss.hasPermi('category:porp:remove')")
    @Log(title = "物品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{propIds}")
    public AjaxResult remove(@PathVariable Long[] propIds)
    {
        return toAjax(sysPorpService.deleteSysPorpByPropIds(propIds));
    }
}
